package gitee.derumin.lib_for_derumin.commands.suggestionproviders;

import com.google.gson.JsonObject;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.suggestion.SuggestionProvider;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import gitee.derumin.lib_for_derumin.helpers.config.ConfigHelper;
import net.minecraft.server.command.ServerCommandSource;

import java.util.concurrent.CompletableFuture;

public class ConfigEntrySuggestionProvider implements SuggestionProvider<ServerCommandSource> {
    public static final ConfigEntrySuggestionProvider PROVIDER = new ConfigEntrySuggestionProvider();
    @Override
    public CompletableFuture<Suggestions> getSuggestions(CommandContext<ServerCommandSource> context, SuggestionsBuilder builder) {
        JsonObject object = null;
        String[] nodes = context.getInput().split(" ");
        for (int i = nodes.length - 2; i > -1; i--){
            if (nodes[i].equals("modify") || nodes[i].equals("get") || nodes[i].equals("reset")){
                object = ConfigHelper.getConfigByName(nodes[i+1], false);
                break;
            }
        }
        if (object != null){
            for (String key : object.keySet()){
                builder.suggest(key);
            }
        }else {
            builder.suggest("no such config");
        }
        return builder.buildFuture();
    }
}
